;; -*- mode: emacs-lisp; coding: gbk-dos; -*-
;; Daniel Carson <edaniel.shen@gmail.com>

;;recentf
(require 'recentf)
(setq recentf-save-file (concat my-emacs-conf-path ".recentf"))
(setq recentf-max-saved-items 500)
(setq recentf-max-menu-items 60)
(global-set-key [(f12)] 'recentf-open-files)
(recentf-mode 1)

(require 'session)
(setq session-save-file (concat my-emacs-conf-path "/.session"))
(add-to-list 'session-globals-exclude 'org-mark-ring)
(add-hook 'after-init-hook 'session-initialize)

(require 'desktop)
(setq desktop-save 'if-exists)
(setq desktop-base-file-name ".desktop")
(setq desktop-base-lock-name ".desktop.lock")
(setq desktop-path (list my-emacs-conf-path))
(setq history-length 250)
(add-to-list 'desktop-globals-to-save 'file-name-history)
(add-to-list 'desktop-globals-to-save 'desktop-missing-file-warning)
(setq desktop-load-locked-desktop t)

(setq desktop-globals-to-save
      (append '((extended-command-history . 30)
                (file-name-history        . 100)
                (grep-history             . 30)
                (compile-history          . 30)
                (minibuffer-history       . 50)
                (query-replace-history    . 60)
                (read-expression-history  . 60)
                (regexp-history           . 60)
                (regexp-search-ring       . 20)
                (search-ring              . 20)
                (shell-command-history    . 50)
                tags-file-name
                register-alist)))

(setq desktop-buffers-not-to-save
      (concat "\\(" "^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS"
              "\\|\\.emacs.*\\|\\.diary\\|\\.newsrc-dribble\\|\\.bbdb" 
              "\\)$"))
(add-to-list 'desktop-modes-not-to-save 'dired-mode)
(add-to-list 'desktop-modes-not-to-save 'Info-mode)
(add-to-list 'desktop-modes-not-to-save 'info-lookup-mode)
(add-to-list 'desktop-modes-not-to-save 'fundamental-mode)

(setq *foo-desktop-file* (concatenate 'string my-emacs-conf-path
                                      "/" 
                                      desktop-base-file-name))

(setq *foo-desktop-lock* (concatenate 'string my-emacs-conf-path
                                      "/" 
                                      desktop-base-lock-name))
(defun desktop-in-use-p ()
  (and (file-exists-p *foo-desktop-file*)
       (file-exists-p *foo-desktop-lock*)))
(defun autosave-desktop ()
  (if (desktop-in-use-p)
      (desktop-save-in-desktop-dir)))
;; ;; Can be switched off with (cancel-timer *foo-desktop-saver-timer*)
(add-hook 'after-init-hook
          (lambda ()
            (setq *foo-desktop-saver-timer* 
                  (run-with-timer 5 300 'autosave-desktop))))
;; (cancel-timer *foo-desktop-saver-timer*)

(desktop-save-mode 1)
(desktop-read)